<?php
	namespace Admin\Controller;
	use Common\Controller\PublicController;
	header("Content-Type:text/html;charset=utf-8");
	class DemandController extends PublicController
	{
		//查看需求
		public function demand_list()
		{
			//获取登录用户id
			$id = session('admin_id');
			//查询需求表
			$demandinfo = M("demand")->where("uid=0")->select();
			
			$this->assign("demandinfo", $demandinfo);
			$this->display();
		}
		//添加需求界面
		public function demand_add()
		{
			//查询身价表
			$spread = M("spread")->select();
			//查询标签表
			$taginfo = M("taginfo")->select();
			
			$this->assign("spread", $spread);
			$this->assign("taginfo", $taginfo);
			$this->display();
		}
		//添加需求
		public function add_demand()
		{
			//获取提交的数据
			$id = session('admin_id');
			$_POST['aid'] = $id;
			$tagname = implode("-", $_POST['tagid']);
			unset($_POST['tagid']);
			$_POST['tagname'] = $tagname;
			
			$dmcontent = addslashes($_POST["dmcontent"]);
			unset($_POST["dmcontent"]);
			$_POST["dmcontent"] = $dmcontent;
			
			//var_dump($_POST);
			//添加需求
			$myfile = $_FILES['dmimg'];
			if ($myfile['name'] != NULL)
			{
				//文件上传
				$upload = new \Think\Upload();// 实例化上传类
				$upload->maxSize   =     3145728 ;// 设置附件上传大小
				$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg','txt');// 设置附件上传类型
				$filename = $myfile['name'];//获得文件名
				$index = strrpos($filename, ".");//对文件名进行“.”截取
				$ext = substr($filename, $index);//获得文件的扩展名
				$newname = md5(date("ymdHis").mt_rand(1000, 9999)).$ext;//重新命名该文件
				$upload->saveName = $newname;
				$upload->autoSub = false;//关闭使用子目录保存
				$upload->rootPath = './Public/Uploads/images/'; // 设置附件上传根目录
				/* $upload->savePath = 'images/'.$newname; */ // 设置附件上传（子）目录
				// 上传单个文件
				$info   =   $upload->upload();
				if(!$info)
				{
					// 上传错误提示错误信息
					$this->error($upload->getError());
				}
				else
				{
					$_POST['dmimg'] = $upload->saveName;
					
				}
			}
			$res = M("Demand") -> data($_POST) -> add();
			//var_dump($res);
			if ($res)
			{
				$this->success("添加需求成功！", U('Demand/demand_add'));
			}
			else
			{
				$this->error("添加需求失败！", U('Demand/demand_add'));
			}
		}
		//需求详情
		public function demand_details()
		{
			//获取登录用户id
			$dmid = $_GET['id'];
			
			//查询身价表
			$spread = M("spread")->select();
			//查询标签表
			$taginfo = M("taginfo")->select();
			//查询需求表
			$demandinfo = M("demand")->where("dmid={$dmid}")->find();

			$this->assign("spread", $spread);
			$this->assign("taginfo", $taginfo);
			$this->assign("demandinfo", $demandinfo);
			$this->display();
		}
		//更新需求
		public function demand_update()
		{
			//获得需要更新的需求id
			$dmid = $_GET['id'];
			M("demand")->startTrans();//开启pdo事务，用于删除失败时回滚事件
			//更新需求
			$res = M("demand")->where("dmid={$dmid}")->save($_POST);
			if ($res > 0) 
			{
				$this->success("修改需求成功！",U("Demand/demand_list"));
			}
			else
			{
				$this->error("修改需求失败", U("Demand/demand_details"));
			}
		}
		//删除需求
		public function demand_delete()
		{
			//获得需求的dmid
			$dmid = $_GET['id'];
			M("demand")->startTrans();
			//删除需求
			$res = M("demand")->where("dmid={$dmid}")->delete();
			if ($res > 0) 
			{
				$this->success("删除需求成功！", U("Demand/demand_list"));
			}
			else
			{
				$this->error("删除需求失败！", U("Demand/demand_list"));
			}	
		}
		//需求查询
		public function demandSelect()
		{
			
		}
	}